﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Log4cb;
using System.IO;
using System.Xml.Linq;
using Protocol;


namespace MDS
{
    static public class Log
    {
        public static Log4cbHelper LogHelper;

        public static int sn = 0;

        static Log()
        {
            //Log4cbHelper.Initialze();
            try
            {
                LogHelper = new Log4cbHelper("MDS");
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public static void LogSN(String ServerName, IMessage V)
        {
            //String str = "";

            //if (V.MsgCode.Value == 80001)
            //{
            //    str = ((GWMKTBidAskANS)V).SequenceNo.Value.ToString();
            //}
            //else if (V.MsgCode.Value == 80011)
            //{
            //    str = ((GWMKTTicksANS)V).SequenceNo.Value.ToString();
            //}
            //else if (V.MsgCode.Value == GWMKTSETTradeSingalANS.MSGCODE)
            //{
            //    str = ((GWMKTSETTradeSingalANS)V).SequenceNo.Value.ToString();
            //}
            //else
            //{
            //    //GWMKTSETTradeSingalANS
            //    return;
            //}
        }

        /// <summary>
        /// 记录捕捉到的异常信息
        /// </summary>
        /// <param name="e"></param>
        public static void Exception(Exception e)  
        {
            //TraceInfo(e);

            //DebugLog.WriteLine(e);

            //try
            //{
            //    LogHelper.LogMsg(LogLevel.Error, e, "{0},{1}", e.Message, e.StackTrace);
            //}
            //catch (Exception)
            //{
            //    //TraceInfo(ee);
            //}
        }

        /// <summary>
        /// 记录状态信息的日志
        /// </summary>
        /// <param name="mess"></param>
        public static void State(String mess)
        {
            //try
            //{
            //    LogHelper.LogMsg(LogLevel.Info, mess);
            //}
            //catch (Exception)
            //{
            //    //TraceInfo(e);
            //}
        }

        /// <summary>
        /// 记录状态信息的日志
        /// </summary>
        /// <param name="mess"></param>
        public static void Debug(String mess)
        {
            try
            {
                LogHelper.LogInfoMsg(Log4cbType.LogFileOnly, mess);
            }
            catch (Exception)
            {
                //TraceInfo(e);
            }
        }
        public static void Debug(Exception ex)
        {
            Debug(ex.StackTrace);
        }

        private static void TraceInfo(Exception e)
        {
            try
            {
            #region 输出

            //int mpos;
            //int fpos;
            //int lpos;

            //foreach (XElement element in xml.Nodes())
            //{
            //    if (element.Name.ToString().Equals("Exception"))
            //    {
            //        XElement childElem = new XElement("TrackInfo");

            //        childElem.SetAttributeValue("id", (++sn).ToString());
            //        childElem.SetAttributeValue("time", DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond);

            //        if (e.InnerException != null)
            //        {
            //            Exception x = InnerExcepTion(e);

            //            mpos = x.StackTrace.IndexOf("在", 0);
            //            fpos = x.StackTrace.IndexOf("位置", mpos);
            //            lpos = x.StackTrace.IndexOf("行号", fpos);

            //            XElement detailElem = new XElement("Detail");
            //            detailElem.SetAttributeValue("type", "原始异常");

            //            detailElem.Add(new XElement("Type", x.GetType().Name));
            //            detailElem.Add(new XElement("Info", x.Message));
            //            detailElem.Add(new XElement("App", x.Source));
            //            detailElem.Add(new XElement("File", FormatPath(x.StackTrace.Substring((fpos + 3), (lpos - fpos - 4)).Trim())));
            //            detailElem.Add(new XElement("Start", x.StackTrace.Substring((mpos + 2), (fpos - mpos - 2)).Trim()));
            //            detailElem.Add(new XElement("Line", x.StackTrace.Substring(lpos + 3).Split(' ')[0].Trim()));
            //            childElem.Add(detailElem);
            //        }

            //        mpos = e.StackTrace.IndexOf("在", 0);
            //        fpos = e.StackTrace.IndexOf("位置", mpos);
            //        lpos = e.StackTrace.IndexOf("行号", fpos);

            //        XElement detailElem1 = new XElement("Detail");
            //        detailElem1.SetAttributeValue("type", "捕获异常");

            //        detailElem1.Add(new XElement("Type", e.GetType().Name));
            //        detailElem1.Add(new XElement("Info", e.Message));
            //        detailElem1.Add(new XElement("App", e.Source));
            //        detailElem1.Add(new XElement("File", FormatPath(e.StackTrace.Substring((fpos + 3), (lpos - fpos - 4)).Trim())));
            //        detailElem1.Add(new XElement("Start", e.StackTrace.Substring((mpos + 2), (fpos - mpos - 2)).Trim()));
            //        detailElem1.Add(new XElement("Line", e.StackTrace.Substring(lpos + 3).Split(' ')[0].Trim()));
            //        childElem.Add(detailElem1);

            //        element.Add(childElem);

            //    }
            //    xml.Save("Log.xml");

            //}
            }
            catch (Exception)
            { }
            #endregion
        }

        private static String FormatPath(String path)
        {
            try
            {
                String[] strs = path.Split('\\');

                return strs[strs.Length - 2] + @"\" + strs[strs.Length - 1];
            }
            catch (Exception)
            { 
            }

            return path;
        }

        private static Exception InnerExcepTion(Exception e)
        {
            if (e == null)
                return new Exception();

            Exception se = e;
            while (se.InnerException != null)
            {
                se = se.InnerException;
            }

            return se;
        }

        public static void Close()
        {
            //xml.Save("Log.xml");

            try
            {
                //Log4cbHelper.StopLogThread();
            }
            catch (Exception)
            { }
        }
    }
}
